<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/html; charset=utf-8");
%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<%--    <base href="<%=basePath %>">--%>
    <meta charset="utf-8" />
    <title>示例</title>
    <link rel="stylesheet" href="./css/editormd.css" />
    <link rel="stylesheet" href="./css/style.css" />
    <script src="../js/jquery-3.4.1.js"></script>
    <script src="./editormd.js"></script>
</head>
<body>
<div id="layout">
    <header>
        <h1>editor.md示例</h1>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </header>
    <div id="test-editormd">
        <textarea style="display:none;" class="form-control" id="editormd" name="editormd"></textarea>
        <textarea class="editormd-html-textarea" name="text" id="editormdData"></textarea>
    </div>
</div>

<script type="text/javascript">
    document.charset = "UTF-8";
    var testEditor;
    $(function() { //Editor初始化：
        testEditor = editormd("test-editormd", {
            width: "90%",
            height: 640,
            syncScrolling: "single",
            // theme : "dark",
            path: "./lib/", //依赖lib文件夹路径
            //这个配置可以让构造出来的HTML代码直接在第二个隐藏的textarea域中，方便post提交表单
            saveHTMLToTextarea: true, // 保存 HTML 到 Textarea，true表示转化为html格式的内容也同样提交到后台
            toolbarAutoFixed: true, //工具栏自动固定定位的开启与禁用
            imageUpload: true,
            imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
            imageUploadURL: "<%=basePath %>imageUpload",
        });
        $("#saveBtn").click(function () {
            var data = $("#editormdData").text();
            var utfData = encodeURIComponent(data);
            // alert(html);
            $.ajax({
                data:{'data':data},
                dataType:'text',
                type:'post',
                contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
                url:'<%=basePath %>saveData',
                success:function (msg) {
                    alert(msg);
                }
            })
        });
    });
</script>
<input id="saveBtn" type="submit" class="btn btn-primary">上传</input>
</body>
</html>
